package com.example.gty.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.example.gty.entity.CourseVo;
import com.example.gty.entity.CourseVo1;
import com.example.gty.service.CourseService;
import com.example.gty.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("/course")
@Api(tags = "课程管理信息")
public class CourseController {
    @Autowired
    private CourseService courseService;

    @ApiOperation("查询所有所有课程及任课老师")
    @RequestMapping(value = "/selectCourseAll",method = RequestMethod.GET)
    public List<CourseVo> selectCourseAll(){
        return courseService.selectCourseAll();
    }

    @ApiOperation("批量添加课程及任课老师")
    @RequestMapping(value = "/addMultiCourse",method = RequestMethod.POST)
    public ResultVo addMultiCourse(@RequestBody MultipartFile file, HttpServletRequest request) throws Exception {
        ServletInputStream inputStream = request.getInputStream();
        ImportParams params = new ImportParams();
        params.setHeadRows(1);//字段行
        params.setTitleRows(1);//标题行


       // FileInputStream in = new FileInputStream("e:/courseAndTea.xlsx");
        InputStream in = file.getInputStream();
        List<CourseVo1> courseVo1 = ExcelImportUtil.importExcel(in, CourseVo1.class, params);
        ResultVo resultVo = courseService.addMultiCourse(courseVo1);
        System.err.println(courseVo1);

       // inputStream.close();
       // in.close();
        return resultVo;

    }

    @ApiOperation("删除课程")
    @RequestMapping(value = "/deleteCourse",method = RequestMethod.GET)
    public ResultVo deleteCourse(String cno,String tno){
        return courseService.deleteCourse(cno);
    }

}
